package com.wuhao.lib_common.websocket.netty

import com.drake.channel.sendEvent
import com.takiku.im_lib.call.Call
import com.takiku.im_lib.dispatcher.Connection
import com.takiku.im_lib.listener.EventListener
import com.wuhao.lib_common.utils.LoggerHelper
import java.io.IOException
import java.net.InetSocketAddress

class WsStatusListener : EventListener() {
    val TAG: String = "NETTY-WEB-SOCKET-STATUS"
    val TAG_HEADER: String = "Netty-WebSocket-Status:"

    /**
     * 连接开始
     *
     * @param inetSocketAddress
     */
    override fun connectStart(inetSocketAddress: InetSocketAddress) {
        super.connectStart(inetSocketAddress)
        LoggerHelper.e(TAG, TAG_HEADER + "connectStart")
    }

    /**
     * 连接成功
     */
    override fun connectSuccess() {
        super.connectSuccess()
        LoggerHelper.e(TAG, TAG_HEADER + "connectSuccess")
    }

    /**
     * 连接出现异常
     *
     * @param throwable
     */
    override fun connectionException(throwable: Throwable) {
        super.connectionException(throwable)
        LoggerHelper.e(TAG, TAG_HEADER + "connectionException")
    }

    /**
     * 连接失败
     *
     * @param inetSocketAddress
     * @param ioe
     */
    override fun connectFailed(inetSocketAddress: InetSocketAddress, ioe: IOException) {
        super.connectFailed(inetSocketAddress, ioe)
        LoggerHelper.e(TAG, TAG_HEADER + "connectFailed")
    }

    /**
     * 连接断开
     */
    override fun connectionBroken() {
        super.connectionBroken()
        LoggerHelper.e(TAG, TAG_HEADER + "connectionBroken")
    }

    /**
     * 连接释放
     *
     * @param connection
     */
    override fun connectionReleased(connection: Connection) {
        super.connectionReleased(connection)
        LoggerHelper.e(TAG, TAG_HEADER + "connectionReleased")
        //断连之后通知主界面进行重连
        sendEvent(WsConnectStatusChangeEvent(false))
    }

    /**
     * 发送开始
     *
     * @param call
     */
    override fun sendMsgStart(call: Call) {
        super.sendMsgStart(call)
        LoggerHelper.e(TAG, TAG_HEADER + "sendMsgStart")
    }

    /**
     * 发送结束
     *
     * @param call
     */
    override fun sendMsgEnd(call: Call) {
        super.sendMsgEnd(call)
        LoggerHelper.e(TAG, TAG_HEADER + "sendMsgEnd")
    }

    /**
     * 发送失败
     *
     * @param call
     */
    override fun sendMsgFailed(call: Call) {
        super.sendMsgFailed(call)
        LoggerHelper.e(TAG, TAG_HEADER + "sendMsgFailed")
    }

    override fun sendHeartStart(call: Call) {
        super.sendHeartStart(call)
        LoggerHelper.e(TAG, TAG_HEADER + "sendHeartStart")
    }
}
